package dbop;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Timer;
import java.util.TimerTask;

import org.apache.log4j.Logger;

public class DBConnectionTester {
	
	private static int test_interval = 20*1000;

	public static volatile boolean dbOK = true;
	
	private static Logger logger = Logger.getLogger(DBConnectionTester.class);
	
	public static void run() {
		TimerTask testTask = new TimerTask() {

			@Override
			public void run() {
				Connection conn = null;
				try {
					logger.info("start test db connection");
					conn = DBManager.generateDBConn();
					logger.info("test connection ok");
					dbOK = true;
				} catch (SQLException e) {
					logger.error("test connection error "+e.getMessage());
					dbOK = false;
				} finally {
					if (conn != null)
						try {
							conn.close();
						} catch (SQLException ignore) {
						}
				}
				
			}
			
		};
		new Timer(true).schedule(testTask, test_interval, test_interval);
	}
	public static void main(String[] args) throws SQLException {
		Connection conn = DBManager.generateDBConn();
		System.out.print("ok");
		
	}
	
}

